<?php
namespace BP\En;
require_once("BP/DA/DBAccess.php");
require_once("BP/DA/DBType.php");
require_once("BP/DA/DBUrlType.php");
require_once("BP/DA/DataColumn.php");
require_once("BP/DA/DataRow.php");
require_once("BP/DA/DataTable.php");
require_once("BP/DA/GroupWay.php");
require_once("BP/DA/OrderWay.php");
require_once("BP/DA/Para.php");
require_once("BP/DA/Paras.php");
require_once("BP/Sys/SystemConfig.php");
require_once("BP/Tools/StringHelper.php");
class QueryObject {
	protected $_en;	// Entity
	protected $_ens;	// Entities
	protected $_sql;	// String
	protected $_groupBy;	// String
	protected $_Top;	// int
	protected $_Paras;	// Paras
	protected $_ParasR;	// Paras
	public $HisDBType;	// BP.DA.DBType
	public $HisDBUrlType;	// BP.DA.DBUrlType
	protected $_orderBy;	// String
	protected $_HisMap;	// Map
	public $IsEndAndOR;	// boolean
	public $FullAttrs;	// String[]
	private function __init() { // default class members
		$this->_en = NULL;
		$this->_ens = NULL;
		$this->_sql = "";
		$this->_groupBy = "";
		$this->_Top = -1;
		$this->_Paras = NULL;
		$this->_ParasR = NULL;
		$this->HisDBType = $DBType->MSSQL;
		$this->HisDBUrlType = $DBUrlType->AppCenterDSN;
		$this->_orderBy = "";
		$this->IsEndAndOR =  FALSE ;
		$this->FullAttrs = NULL;
	}
	protected function getEn () 
	{
		if (($this->_en == NULL))
		{
			return $this->getEns()->getNewEntity();
		}
		else
		{
			return $this->_en;
		}
	}
	protected function getEns () 
	{
		return $this->_ens;
	}
	public function getSQL () 
	{
		$sql = "";
		$selecSQL = SqlBuilder::SelectSQL($this->getEn(), $this->getTop());
		if ((($this->_sql == NULL) || ($this->_sql->length() == 0)))
		{
			$sql = (($selecSQL . $this->_groupBy) . $this->_orderBy);
		}
		else
		{
			if ($selecSQL->contains(" WHERE "))
			{
				$sql = ((((($selecSQL . "  AND ( ") . $this->_sql) . " ) ") . $this->_groupBy) . $this->_orderBy);
			}
			else
			{
				$sql = ((((($selecSQL . " WHERE   ( ") . $this->_sql) . " ) ") . $this->_groupBy) . $this->_orderBy);
			}
		}
		$sql = $sql->replace("  ", " ");
		$sql = $sql->replace("  ", " ");
		$sql = $sql->replace("WHERE AND", "WHERE");
		$sql = $sql->replace("WHERE  AND", "WHERE");
		$sql = $sql->replace("WHERE ORDER", "ORDER");
		return $sql;
	}
	public function setSQL ($value) // [String value]
	{
		if (($value->indexOf("(") == -1))
		{
			$this->IsEndAndOR =  FALSE ;
		}
		else
		{
			$this->IsEndAndOR =  TRUE ;
		}
		$this->_sql = (($this->_sql . " ") . $value);
	}
	public function getSQLWithOutPara () 
	{
		$sql = $this->getSQL();
		foreach ($this->getMyParas() as $en) 		{
			$sql = $sql->replace(($SystemConfig->getAppCenterDBVarStr() + $en->ParaName), (("'" . $en->val) . "'"));
		}
		return $sql;
	}
	public function AddWhere_String ($str) // [String str]
	{
		$this->_sql = (($this->_sql . " ") . $str);
	}
	public function getTop () 
	{
		return $this->_Top;
	}
	public function setTop ($value) // [int value]
	{
		$this->_Top = $value;
	}
	public function getMyParas () 
	{
		if (($this->_Paras == NULL))
		{
			$this->_Paras = new Paras();
		}
		return $this->_Paras;
	}
	public function setMyParas ($value) // [Paras value]
	{
		$this->_Paras = $value;
	}
	public function getMyParasR () 
	{
		if (($this->_ParasR == NULL))
		{
			$this->_ParasR = new Paras();
		}
		return $this->_ParasR;
	}
	public function AddPara ($key, $v) // [String key, Object v]
	{
		$key = ("P" . $key);
		$this->getMyParas()->Add($key, $v);
	}
	public static function constructor__ () 
	{
		$me = new self();
		$me->__init();
		return $me;
	}
	public static function constructor__Entity ($en) // [Entity en]
	{
		$me = new self();
		$me->__init();
		$me->getMyParas()->clear();
		$me->_en = $en;
		$me->HisDBType = $me->_en->getEnMap()->getEnDBUrl()->getDBType();
		$me->HisDBUrlType = $me->_en->getEnMap()->getEnDBUrl()->getDBUrlType();
		return $me;
	}
	public static function constructor__Entities ($ens) // [Entities ens]
	{
		$me = new self();
		$me->__init();
		$me->getMyParas()->clear();
		$ens->clear();
		$me->_ens = $ens;
		$en = $me->_ens->getNewEntity();
		$me->HisDBType = $SystemConfig->getAppCenterDBType();
		$me->HisDBUrlType = $en->getEnMap()->getEnDBUrl()->getDBUrlType();
		return $me;
	}
	public function getHisVarStr () 
	{
		switch ($this->HisDBType) {
			case $MSSQL:
			case $Access:
				return ":";
			case $MySQL:
				return ":";
			case $Informix:
				return "?";
			default:
				return ":";
		}
	}
	public function AddWhereLen ($attr, $exp, $len, $dbtype) // [String attr, String exp, int len, BP.DA.DBType dbtype]
	{
		$this->setSQL((((((((("( " . SystemConfig::getAppCenterDBLengthStr()) . "( ") . $this->attr2Field($attr)) . " ) ") . $exp) . " '") . (new Integer($len))) . "')"));
	}
	public function AddWhereInSQL_String_String ($attr, $sql) // [String attr, String sql]
	{
		$this->AddWhere_String_String_String($attr, " IN ", (("( " . $sql) . " )"));
	}
	public function AddWhereExistsSQL ($attr, $sql) // [String attr, String sql]
	{
		$this->AddWhere_String_String_String($attr, " EXISTS ", (("( " . $sql) . " )"));
	}
	public function AddWhereInSQL_String_String_String ($attr, $sql, $orderBy) // [String attr, String sql, String orderBy]
	{
		$this->AddWhere_String_String_String($attr, " IN ", (("( " . $sql) . " )"));
			/* match: String */
		$this->addOrderBy_String($orderBy);
	}
	public function AddWhereNotInSQL ($attr, $sql) // [String attr, String sql]
	{
		$this->AddWhere_String_String_String($attr, " NOT IN ", ((" ( " . $sql) . " ) "));
	}
	public function AddWhereNotIn ($attr, $val) // [String attr, String val]
	{
		$this->AddWhere_String_String_String($attr, " NOT IN ", ((" ( " . $val) . " ) "));
	}
	public function AddWhereIn_String_DataTable ($attr, $dt) // [String attr, DataTable dt]
	{
		$strs = "";
		foreach ($dt->Rows as $dr) 		{
			$strs += ($dr->getValue(0) . ",");
		}
		$strs = $strs->substring(($strs->length() - 1), (($strs->length() - 1) + 0));
		$this->AddWhereIn_String_String($attr, $strs);
	}
	public function AddWhereIn_String_String ($attr, $vals) // [String attr, String vals]
	{
		$this->AddWhere_String_String_String($attr, " IN ", $vals);
	}
	public function AddWhere_String_String_String ($attr, $exp, $val) // [String attr, String exp, String val]
	{
			/* match: String_String_String_String */
		$this->AddWhere_String_String_String_String($attr, $exp, $val, NULL);
	}
	public function AddWhere_String_String_String_String ($attr, $exp, $val, $paraName) // [String attr, String exp, String val, String paraName]
	{
		if (($val . NULL))
			$val = "";
		if ($val->equals("all"))
		{
			$this->setSQL("( 1=1 )");
			return ;
		}
		if ($exp->toLowerCase()->contains("in"))
		{
			$this->setSQL((((((("( " . $this->attr2Field($attr)) . " ") . $exp) . "  ") . $val) . " )"));
			return ;
		}
		if ($exp->toLowerCase()->contains(" exists"))
		{
			$sql = new StringBuilder();
			$sql->append(" EXISTS ( ");
			$sql->append("	SELECT 1 FROM ( ");
			$sql->append($val);
			$sql->append("	) JFLOW_ ");
			$sql->append("	WHERE ");
			$sql->append("		JFLOW_.")->append($attr)->append("=")->append($this->attr2Field($attr));
			$sql->append(" ) ");
			$this->setSQL((("( " . $sql) . " )"));
			return ;
		}
		if ($exp->toLowerCase()->contains("like"))
		{
			if ($attr->equals("FK_Dept"))
			{
				$val = $val->replace("'", "");
				$val = $val->replace("%", "");
				switch ($this->HisDBType) {
					case $Oracle:
						$this->setSQL((((((("( " . $this->attr2Field($attr)) . " ") . $exp) . " '%'||") . $this->getHisVarStr()) . "FK_Dept||'%' )"));
						$this->getMyParas()->Add("FK_Dept", $val);
						break;
					default:
						$this->setSQL((((((("( " . $this->attr2Field($attr)) . " ") . $exp) . "  '") . $val) . "%' )"));
						break;
				}
			}
			else
			{
				if ((($val->indexOf(":") == 0) || ($val->indexOf("@") == 0)))
				{
					$this->setSQL((((((("( " . $this->attr2Field($attr)) . " ") . $exp) . "  ") . $val) . " )"));
				}
				else
				{
					if (!$val->contains("'"))
					{
						$this->setSQL((((((("( " . $this->attr2Field($attr)) . " ") . $exp) . "  '") . $val) . "' )"));
					}
					else
					{
						$this->setSQL((((((("( " . $this->attr2Field($attr)) . " ") . $exp) . "  ") . $val) . " )"));
					}
				}
			}
			return ;
		}
		if ($this->getHisVarStr()->equals("?"))
		{
			$this->setSQL((((("( " . $this->attr2Field($attr)) . " ") . $exp) . "?)"));
			$this->getMyParas()->Add($attr, $val);
		}
		else
		{
			if (($paraName . NULL))
			{
				$this->setSQL((((((("( " . $this->attr2Field($attr)) . " ") . $exp) . $this->getHisVarStr()) . $attr) . ")"));
				$this->getMyParas()->Add($attr, $val);
			}
			else
			{
				$this->setSQL((((((("( " . $this->attr2Field($attr)) . " ") . $exp) . $this->getHisVarStr()) . $paraName) . ")"));
				$this->getMyParas()->Add($paraName, $val);
			}
		}
	}
	public function AddWhereDept ($val) // [String val]
	{
		$attr = "FK_Dept";
		$exp = "=";
		if (!$val->contains("'"))
		{
			$this->setSQL((((((("( " . $this->attr2Field($attr)) . " ") . $exp) . "  '") . $val) . "' )"));
		}
		else
		{
			$this->setSQL((((((("( " . $this->attr2Field($attr)) . " ") . $exp) . "  ") . $val) . " )"));
		}
	}
	public function AddWhereIsNull ($attr) // [String attr]
	{
		$this->setSQL((((("( " . $this->attr2Field($attr)) . "  IS NULL OR  ") . $this->attr2Field($attr)) . "='' )"));
	}
	public function AddWhereField_String_String_String ($attr, $exp, $val) // [String attr, String exp, String val]
	{
		if ($val->equals("all"))
		{
			$this->setSQL("( 1=1 )");
			return ;
		}
		if ($exp->toLowerCase()->contains(" in"))
		{
			$this->setSQL((((((("( " . $attr) . " ") . $exp) . "  ") . $val) . " )"));
			return ;
		}
		$this->setSQL((((((("( " . $attr) . " ") . $exp) . " :") . $attr) . " )"));
		$this->getMyParas()->Add($attr, $val);
	}
	public function AddWhereField_String_String_I ($attr, $exp, $val) // [String attr, String exp, int val]
	{
		if ((new Integer($val))->equals("all"))
		{
			$this->setSQL("( 1=1 )");
			return ;
		}
		if ($exp->toLowerCase()->contains(" in"))
		{
			$this->setSQL((((((("( " . $attr) . " ") . $exp) . "  ") . $val) . " )"));
			return ;
		}
		if ($attr->equals("RowNum"))
		{
			$this->setSQL((((((("( " . $attr) . " ") . $exp) . "  ") . $val) . " )"));
			return ;
		}
		if ($this->getHisVarStr()->equals("?"))
		{
			$this->setSQL((((("( " . $attr) . " ") . $exp) . "?)"));
		}
		else
		{
			$this->setSQL(((((((("( " . $attr) . " ") . $exp) . "  ") . $this->getHisVarStr()) . $attr) . " )"));
		}
		$this->getMyParas()->Add($attr, $val);
	}
	public function AddWhere_String_String_I ($attr, $exp, $val) // [String attr, String exp, int val]
	{
		if ($attr->equals("RowNum"))
		{
			$this->setSQL((((((("( " . $this->attr2Field($attr)) . " ") . $exp) . " ") . $val) . ")"));
		}
		else
		{
			if ($this->getHisVarStr()->equals("?"))
			{
				$this->setSQL((((("( " . $this->attr2Field($attr)) . " ") . $exp) . "?)"));
			}
			else
			{
				$this->setSQL((((((("( " . $this->attr2Field($attr)) . " ") . $exp) . $this->getHisVarStr()) . $attr) . ")"));
			}
			$this->getMyParas()->Add($attr, $val);
		}
	}
	public function AddHD () 
	{
		$this->setSQL("(  1=1 ) ");
	}
	public function AddHD_Not () 
	{
		$this->setSQL("(  1=2 ) ");
	}
	public function AddWhere_String_String_F ($attr, $exp, $val) // [String attr, String exp, float val]
	{
		$this->getMyParas()->Add($attr, $val);
		if ($this->getHisVarStr()->equals("?"))
		{
			$this->setSQL((((("( " . $this->attr2Field($attr)) . " ") . $exp) . "?)"));
		}
		else
		{
			$this->setSQL(((((((("( " . $this->attr2Field($attr)) . " ") . $exp) . " ") . $this->getHisVarStr()) . $attr) . ")"));
		}
	}
	public function AddWhere_String_String ($attr, $val) // [String attr, String val]
	{
		$this->AddWhere_String_String_String($attr, "=", $val);
	}
	public function AddWhere_String_I ($attr, $val) // [String attr, int val]
	{
		$this->AddWhere_String_String_I($attr, "=", $val);
	}
	public function AddWhere_String_b ($attr, $val) // [String attr, boolean val]
	{
		if ($val)
		{
			$this->AddWhere_String_String_I($attr, "=", 1);
		}
		else
		{
			$this->AddWhere_String_String_I($attr, "=", 0);
		}
	}
	public function AddWhere_String_L ($attr, $val) // [String attr, long val]
	{
		$this->AddWhere_String_F($attr, (new Long($val)));
	}
	public function AddWhere_String_F ($attr, $val) // [String attr, float val]
	{
		$this->AddWhere_String_String_F($attr, "=", $val);
	}
	public function AddWhere_String_Object ($attr, $val) // [String attr, Object val]
	{
		if ($val instanceof Enum)
		{
			$this->AddWhere_String_F($attr, ($val)->ordinal());
			return ;
		}
		if (($val == NULL))
		{
			throw new \RuntimeException((("Attr=" . $attr) . ", val is null"));
		}
		if ((($val->getClass() == Integer) || ($val->getClass() == Integer)))
		{
			$this->AddWhere_String_String_F($attr, "=", ($val)->intValue());
			return ;
		}
		$this->AddWhere_String_String_F($attr, "=", $val);
	}
	public function addLeftBracket () 
	{
		$this->setSQL(" ( ");
	}
	public function addRightBracket () 
	{
		$this->setSQL(" ) ");
		$this->IsEndAndOR =  TRUE ;
	}
	public function addAnd () 
	{
		$this->setSQL(" AND ");
	}
	public function addOr () 
	{
		$this->setSQL(" OR ");
	}
	public function addGroupBy_String ($attr) // [String attr]
	{
		$this->_groupBy = (" GROUP BY  " . $this->attr2Field($attr));
	}
	public function addGroupBy_String_String ($attr1, $attr2) // [String attr1, String attr2]
	{
		$this->_groupBy = (((" GROUP BY  " . $this->attr2Field($attr1)) . " , ") . $this->attr2Field($attr2));
	}
	public function addOrderBy_String ($attr) // [String attr]
	{
		if (($this->_orderBy->indexOf("ORDER BY") != -1))
		{
			$this->_orderBy = (" , " . $this->attr2Field($attr));
		}
		else
		{
			$this->_orderBy = (" ORDER BY " . $this->attr2Field($attr));
		}
	}
	public function addOrderByRandom () 
	{
		if (($this->_orderBy->indexOf("ORDER BY") != -1))
		{
			$this->_orderBy = " , NEWID()";
		}
		else
		{
			$this->_orderBy = " ORDER BY NEWID()";
		}
	}
	public function addOrderByDesc_String ($attr) // [String attr]
	{
		$this->_orderBy = ((" ORDER BY " . $this->attr2Field($attr)) . " DESC ");
	}
	public function addOrderByDesc_String_String ($attr1, $attr2) // [String attr1, String attr2]
	{
		$this->_orderBy = ((((" ORDER BY  " . $this->attr2Field($attr1)) . " DESC ,") . $this->attr2Field($attr2)) . " DESC");
	}
	public function addOrderBy_String_String ($attr1, $attr2) // [String attr1, String attr2]
	{
		$this->_orderBy = (((" ORDER BY  " . $this->attr2Field($attr1)) . ",") . $this->attr2Field($attr2));
	}
	abstract function addHaving (); 
	public function clear () 
	{
		$this->_sql = "";
		$this->_groupBy = "";
		$this->getMyParas()->clear();
	}
	public function getHisMap () 
	{
		if (($this->_HisMap == NULL))
		{
			$this->_HisMap = $this->getEn()->getEnMap();
		}
		return $this->_HisMap;
	}
	public function setHisMap ($value) // [Map value]
	{
		$this->_HisMap = $value;
	}
	protected function attr2Field ($attrKey) // [String attrKey]
	{
		$attr = $this->getHisMap()->GetAttrByKey($attrKey);
		if (($attr->getIsRefAttr() ==  TRUE ))
		{
			if (($this->HisDBType == $DBType->Oracle))
			{
				return (("T" . $attr->getKey()->replace("Text", "")) . ".Name");
			}
			else
			{
				$en = $attr->getHisFKEn();
				return ((($en->getEnMap()->getPhysicsTable() . "_") . $attr->getKey()->replace("Text", "")) . ".Name");
			}
		}
		return (($this->getHisMap()->getPhysicsTable() . ".") . $attr->getField());
	}
	public function DoGroupReturnTable ($en, $attrsOfGroupKey, $attrGroup, $gw, $ow) // [Entity en, Attrs attrsOfGroupKey, Attr attrGroup, GroupWay gw, OrderWay ow]
	{
		switch ($en->getEnMap()->getEnDBUrl()->getDBType()) {
			case $Oracle:
				return $this->DoGroupReturnTableOracle($en, $attrsOfGroupKey, $attrGroup, $gw, $ow);
			default:
				return $this->DoGroupReturnTableSqlServer($en, $attrsOfGroupKey, $attrGroup, $gw, $ow);
		}
	}
	public function DoGroupReturnTableOracle ($en, $attrsOfGroupKey, $attrGroup, $gw, $ow) // [Entity en, Attrs attrsOfGroupKey, Attr attrGroup, GroupWay gw, OrderWay ow]
	{
		$fields = "";
		$str = "";
		foreach ($attrsOfGroupKey as $attr) 		{
			if (($attr->getField() == NULL))
			{
				continue;
			}
			$str = ("," . $attr->getField());
			$fields += $str;
		}
		if ($attrGroup->getKey()->equals("MyNum"))
		{
			switch ($gw) {
				case $BySum:
					$fields += ", COUNT(*) AS MyNum";
					break;
				case $ByAvg:
					$fields += ((", AVG(" . $attrGroup->getField()) . ") AS MyNum");
					break;
				default:
					throw new \RuntimeException("no such case:");
			}
		}
		else
		{
			switch ($gw) {
				case $BySum:
					$fields += (((",SUM(" . $attrGroup->getField()) . ") AS ") . $attrGroup->getKey());
					break;
				case $ByAvg:
					$fields += (((",AVG(" . $attrGroup->getField()) . ") AS ") . $attrGroup->getKey());
					break;
				default:
					throw new \RuntimeException("no such case:");
			}
		}
		$by = "";
		foreach ($attrsOfGroupKey as $attr) 		{
			if (($attr->getField() == NULL))
			{
				continue;
			}
			$str = ("," . $attr->getField());
			$by += $str;
		}
		$by = $by->substring(1);
		$sql = ((((((("SELECT " . $fields->substring(1)) . " FROM ") . $this->getEn()->getEnMap()->getPhysicsTable()) . " WHERE ") . $this->_sql) . " Group BY ") . $by);
		$map = Map::constructor__();
		$map->setPhysicsTable("@VT@");
		$map->setAttrs($attrsOfGroupKey);
			/* match: Attr */
		$map->getAttrs()->Add_Attr($attrGroup);
			/* match: Entity_Map */
		$sql1 = ((SqlBuilder->SelectSQLOfOra_String_Map($en, $map) . " ") . SqlBuilder->GenerFormWhereOfOra_Entity_Map($en, $map));
		$sql1 = $sql1->replace("@TopNum", "");
		$sql1 = $sql1->replace("FROM @VT@", (("FROM (" . $sql) . ") VT"));
		$sql1 = $sql1->replace("@VT@", "VT");
		$sql1 = $sql1->replace("TOP", "");
		if (($ow == $OrderWay->OrderByUp))
		{
			$sql1 += ((" ORDER BY " . $attrGroup->getKey()) . " DESC ");
		}
		else
		{
			$sql1 += (" ORDER BY " . $attrGroup->getKey());
		}
		return DBAccess->RunSQLReturnTable($sql1, $this->getMyParas());
	}
	public function DoGroupReturnTableSqlServer ($en, $attrsOfGroupKey, $attrGroup, $gw, $ow) // [Entity en, Attrs attrsOfGroupKey, Attr attrGroup, GroupWay gw, OrderWay ow]
	{
		$fields = "";
		$str = "";
		foreach ($attrsOfGroupKey as $attr) 		{
			if (($attr->getField() == NULL))
			{
				continue;
			}
			$str = ("," . $attr->getField());
			$fields += $str;
		}
		if ($attrGroup->getKey()->equals("MyNum"))
		{
			switch ($gw) {
				case $BySum:
					$fields += ", COUNT(*) AS MyNum";
					break;
				case $ByAvg:
					$fields += ", AVG(*)   AS MyNum";
					break;
				default:
					throw new \RuntimeException("no such case:");
			}
		}
		else
		{
			switch ($gw) {
				case $BySum:
					$fields += (((",SUM(" . $attrGroup->getField()) . ") AS ") . $attrGroup->getKey());
					break;
				case $ByAvg:
					$fields += (((",AVG(" . $attrGroup->getField()) . ") AS ") . $attrGroup->getKey());
					break;
				default:
					throw new \RuntimeException("no such case:");
			}
		}
		$by = "";
		foreach ($attrsOfGroupKey as $attr) 		{
			if (($attr->getField() == NULL))
			{
				continue;
			}
			$str = ("," . $attr->getField());
			$by += $str;
		}
		$by = $by->substring(1);
		$sql = ((((((("SELECT " . $fields->substring(1)) . " FROM ") . $this->getEn()->getEnMap()->getPhysicsTable()) . " WHERE ") . $this->_sql) . " Group BY ") . $by);
		$map = Map::constructor__();
		$map->setPhysicsTable("@VT@");
		$map->setAttrs($attrsOfGroupKey);
			/* match: Attr */
		$map->getAttrs()->Add_Attr($attrGroup);
			/* match: Map */
			/* match: Entity_Map */
		$sql1 = ((SqlBuilder->SelectSQLOfMS_Map($map) . " ") . SqlBuilder->GenerFormWhereOfMS_Entity_Map($en, $map));
		$sql1 = $sql1->replace("@TopNum", "");
		$sql1 = $sql1->replace("FROM @VT@", (("FROM (" . $sql) . ") VT"));
		$sql1 = $sql1->replace("@VT@", "VT");
		$sql1 = $sql1->replace("TOP", "");
		if (($ow == $OrderWay->OrderByUp))
		{
			$sql1 += ((" ORDER BY " . $attrGroup->getKey()) . " DESC ");
		}
		else
		{
			$sql1 += (" ORDER BY " . $attrGroup->getKey());
		}
		return DBAccess->RunSQLReturnTable($sql1, $this->getMyParas());
	}
	public function DoQuery () 
	{
		try 
		{
			if (($this->_en == NULL))
			{
				return $this->doEntitiesQuery();
			}
			else
			{
				return $this->doEntityQuery();
			}
		}
		catch (\RuntimeException $ex)
		{
			if (($this->_en == NULL))
			{
				try 
				{
					$this->_ens->getNewEntity()->CheckPhysicsTable();
					return $this->doEntitiesQuery();
				}
				catch (Exception $e)
				{
					$e->printStackTrace();
				}
			}
			else
			{
				try 
				{
					$this->_en->CheckPhysicsTable();
					return $this->doEntityQuery();
				}
				catch (Exception $e)
				{
					$e->printStackTrace();
				}
			}
			throw $ex;
		}
	}
	public function DealString ($sql) // [String sql]
	{
		$dt = DBAccess->RunSQLReturnTable($sql);
		$strs = "";
		foreach ($dt->Rows as $dr) 		{
			$strs += ((",'" . $dr->getValue(0)) . "'");
		}
		return $strs->substring(1);
	}
	public function GenerPKsByTableWithPara ($pk, $sql, $from, $to) // [String pk, String sql, int from, int to]
	{
		$dt = DBAccess->RunSQLReturnTable($sql, $this->getMyParas());
		$pks = "";
		$i = 0;
		$paraI = 0;
		$dbStr = $SystemConfig->getAppCenterDBVarStr();
		foreach ($dt->Rows as $dr) 		{
			++$i;
			if (($i > $from))
			{
				++$paraI;
				if ($dbStr->equals("?"))
				{
					$pks += "?,";
				}
				else
				{
					$pks += ((($SystemConfig->getAppCenterDBVarStr() . "R") . $paraI) . ",");
				}
				if ((($pk->equals("OID") || $pk->equals("WorkID")) || $pk->equals("NodeID")))
					$this->getMyParasR()->Add(("R" . $paraI), $Integer->parseInt($dr->getValue(0)));
				else
					$this->getMyParasR()->Add(("R" . $paraI), $dr->getValue(0));
				if (($i >= $to))
				{
					return $pks->substring(0, ($pks->length() - 1));
				}
			}
		}
		if ($pks->equals(""))
		{
			return NULL;
		}
		return $pks->substring(0, ($pks->length() - 1));
	}
	public function GenerPKsByTable ($sql, $from, $to) // [String sql, int from, int to]
	{
		$dt = DBAccess->RunSQLReturnTable($sql, $this->getMyParas());
		$pks = "";
		$i = 0;
		foreach ($dt->Rows as $dr) 		{
			++$i;
			if (($i > $from))
			{
				if (($i >= $to))
				{
					$pks += (("'" . $dr->getValue(0)) . "'");
					return $pks;
				}
				else
				{
					$pks += (("'" . $dr->getValue(0)) . "',");
				}
			}
		}
		if ($pks->equals(""))
		{
			return "  '11111111' ";
		}
		return $pks->substring(0, ($pks->length() - 1));
	}
	public function DoQuery_String_I_I ($pk, $pageSize, $pageIdx) // [String pk, int pageSize, int pageIdx]
	{
		if (($pk->equals("OID") || $pk->equals("WorkID")))
		{
			return $this->DoQuery_String_I_I_String_b($pk, $pageSize, $pageIdx, $pk,  TRUE );
		}
		else
		{
			return $this->DoQuery_String_I_I_String_b($pk, $pageSize, $pageIdx, $pk,  FALSE );
		}
	}
	public function DoQuery_String_I_I_String_String ($pk, $pageSize, $pageIdx, $orderBy, $orderWay) // [String pk, int pageSize, int pageIdx, String orderBy, String orderWay]
	{
		if ($orderWay->toLowerCase()->trim()->equals("up"))
		{
			return $this->DoQuery_String_I_I_String_b($pk, $pageSize, $pageIdx, $orderBy,  FALSE );
		}
		else
		{
			return $this->DoQuery_String_I_I_String_b($pk, $pageSize, $pageIdx, $orderBy,  TRUE );
		}
	}
	public function DoQuery_String_I_I_b ($pk, $pageSize, $pageIdx, $isDesc) // [String pk, int pageSize, int pageIdx, boolean isDesc]
	{
		return $this->DoQuery_String_I_I_String_b($pk, $pageSize, $pageIdx, $pk, $isDesc);
	}
	public function DoQuery_String_I_I_String_b ($pk, $pageSize, $pageIdx, $orderBy, $isDesc) // [String pk, int pageSize, int pageIdx, String orderBy, boolean isDesc]
	{
		$pageNum = 0;
		if (is_null($this->_orderBy) || $this->_orderBy == '')
		{
			$isDescStr = "";
			if ($isDesc)
			{
				$isDescStr = " DESC ";
			}
			if (is_null($orderBy) || $orderBy == '')
			{
				$orderBy = $pk;
			}
			$this->_orderBy = ($this->attr2Field($orderBy) . $isDescStr);
		}
		if (!$this->_orderBy->contains("ORDER BY"))
		{
			$this->_orderBy = (" ORDER BY " . $this->_orderBy);
		}
		try 
		{
			if (($this->_en == NULL))
			{
				$recordConut = 0;
				$recordConut = $this->GetCount();
				if (($recordConut == 0))
				{
					$this->_ens->clear();
					return 0;
				}
				if (($pageSize == 0))
				{
					$pageSize = 12;
				}
				$pageNum = round($recordConut / $pageSize);
				++$pageNum;
				$top = ($pageSize * (($pageIdx - 1)));
				$sql = "";
				$en = $this->_ens->getNewEntity();
				$map = $en->getEnMap();
				$toIdx = 0;
				$pks = "";
				switch ($map->getEnDBUrl()->getDBType()) {
					case $Oracle:
						$toIdx = ($top + $pageSize);
						if ((($this->_sql == NULL) || $this->_sql->equals("")))
						{
							if (($top == 0))
							{
								$sql = ((((((("SELECT * FROM ( SELECT  " . $pk) . " FROM ") . $map->getPhysicsTable()) . " ") . $this->_orderBy) . "   ) WHERE ROWNUM <=") . $pageSize);
							}
							else
							{
								$sql = (((((("SELECT * FROM ( SELECT  " . $pk) . " FROM ") . $map->getPhysicsTable()) . " ") . $this->_orderBy) . ") ");
							}
						}
						else
						{
							$mySql = $this->getSQL();
							$mySql = $mySql->substring($mySql->indexOf("FROM"));
							if (($top == 0))
							{
								$sql = ((((((("SELECT * FROM ( SELECT  " . $map->getPhysicsTable()) . ".") . $pk) . " ") . $mySql) . " ) WHERE ROWNUM <=") . $pageSize);
							}
							else
							{
								$sql = (((((("SELECT * FROM ( SELECT  " . $map->getPhysicsTable()) . ".") . $pk) . " ") . $mySql) . " )");
							}
						}
						$sql = $sql->replace("AND ( ( 1=1 ) )", " ");
						$pks = $this->GenerPKsByTableWithPara($pk, $sql, $top, $toIdx);
						$this->clear();
						$this->setMyParas($this->getMyParasR());
						if (($pks . NULL))
						{
							$this->AddWhereIn_String_String($pk, (("(" . $pks) . ")"));
						}
						else
						{
							$this->AddHD();
						}
						$this->setTop($pageSize);
						return $this->doEntitiesQuery();
					case $Informix:
						$toIdx = ($top + $pageSize);
						if ((($this->_sql == NULL) || $this->_sql->equals("")))
						{
							if (($top == 0))
							{
								$sql = (((((((" SELECT first  " . $pageSize) . "  ") . $this->getEn()->getPK()) . " FROM ") . $map->getPhysicsTable()) . " ") . $this->_orderBy);
							}
							else
							{
								$sql = (((((" SELECT  " . $this->getEn()->getPK()) . " FROM ") . $map->getPhysicsTable()) . " ") . $this->_orderBy);
							}
						}
						else
						{
							$mySql = $this->getSQL();
							$mySql = $mySql->substring($mySql->indexOf("FROM"));
							if (($top == 0))
							{
								$sql = ((((("SELECT first " . $pageSize) . " ") . $this->getEn()->getPK()) . " ") . $mySql);
							}
							else
							{
								$sql = ((("SELECT  " . $this->getEn()->getPK()) . " ") . $mySql);
							}
						}
						$sql = $sql->replace("AND ( ( 1=1 ) )", " ");
						$pks = $this->GenerPKsByTableWithPara($pk, $sql, $top, $toIdx);
						$this->clear();
						$this->setMyParas($this->getMyParasR());
						if (($pks . NULL))
						{
							$this->AddHD_Not();
						}
						else
						{
							$this->AddWhereIn_String_String($pk, (("(" . $pks) . ")"));
						}
						$this->setTop($pageSize);
						return $this->doEntitiesQuery();
					case $MySQL:
						$toIdx = ($top + $pageSize);
						if ((($this->_sql == NULL) || $this->_sql->equals("")))
						{
							if (($top == 0))
							{
								$sql = (((((((" SELECT  " . $this->getEn()->getPK()) . " FROM ") . $map->getPhysicsTable()) . " ") . $this->_orderBy) . " LIMIT ") . $pageSize);
							}
							else
							{
								$sql = (((((" SELECT  " . $this->getEn()->getPK()) . " FROM ") . $map->getPhysicsTable()) . " ") . $this->_orderBy);
							}
						}
						else
						{
							$mySql = $this->getSQL();
							$mySql = $mySql->substring($mySql->indexOf("FROM"));
							if (($top == 0))
							{
								$sql = ((((((("SELECT  " . $map->getPhysicsTable()) . ".") . $this->getEn()->getPK()) . " ") . $mySql) . " LIMIT ") . $pageSize);
							}
							else
							{
								$sql = ((((("SELECT  " . $map->getPhysicsTable()) . ".") . $this->getEn()->getPK()) . " ") . $mySql);
							}
						}
						$sql = $sql->replace("AND ( ( 1=1 ) )", " ");
						$pks = $this->GenerPKsByTableWithPara($pk, $sql, $top, $toIdx);
						$this->clear();
						$this->setMyParas($this->getMyParasR());
						if (($pks . NULL))
						{
							$this->AddHD_Not();
						}
						else
						{
							$this->AddWhereIn_String_String($pk, (("(" . $pks) . ")"));
						}
						$this->setTop($pageSize);
						return $this->doEntitiesQuery();
					case $MSSQL:
					default:
						$toIdx = ($top + $pageSize);
						if ((($this->_sql == NULL) || $this->_sql->equals("")))
						{
							$sql = (((((" SELECT  [" . $this->getEn()->getPK()) . "] FROM ") . $map->getPhysicsTable()) . " ") . $this->_orderBy);
						}
						else
						{
							$mySql = $this->getSQL();
							$mySql = $mySql->substring($mySql->indexOf("FROM"));
							$sql = ((((((("SELECT " . $map->getPhysicsTable()) . ".") . $this->getEn()->getPK()) . " as  [") . $this->getEn()->getPK()) . "]  ") . $mySql);
						}
						$sql = $sql->replace("AND ( ( 1=1 ) )", " ");
						$pks = $this->GenerPKsByTableWithPara($pk, $sql, $top, $toIdx);
						$this->clear();
						$this->setMyParas($this->getMyParasR());
						if (($pks . NULL))
						{
							$this->AddHD_Not();
						}
						else
						{
							$this->AddWhereIn_String_String($pk, (("(" . $pks) . ")"));
						}
						$this->setTop($pageSize);
						return $this->doEntitiesQuery();
				}
			}
			else
			{
				return $this->doEntityQuery();
			}
		}
		catch (\RuntimeException $ex)
		{
			try 
			{
				if (($this->_en == NULL))
				{
					$this->getEns()->getNewEntity()->CheckPhysicsTable();
				}
				else
				{
					$this->_en->CheckPhysicsTable();
				}
			}
			catch (\Exception $e)
			{ /* empty */ }
			throw $ex;
		}
	}
	public function DoQueryToTable () 
	{
		try 
		{
			$sql = $this->getSQL();
			$sql = $sql->replace("WHERE (1=1) AND ( AND ( ( ( 1=1 ) ) AND ( ( 1=1 ) ) ) )", "");
			return DBAccess->RunSQLReturnTable($sql, $this->getMyParas());
		}
		catch (\RuntimeException $ex)
		{
			if (($this->_en == NULL))
			{
				try 
				{
					$this->getEns()->getNewEntity()->CheckPhysicsTable();
				}
				catch (Exception $e)
				{
					$e->printStackTrace();
				}
			}
			else
			{
				try 
				{
					$this->_en->CheckPhysicsTable();
				}
				catch (Exception $e)
				{
					$e->printStackTrace();
				}
			}
			throw $ex;
		}
	}
	public function GetCount () 
	{
		$sql = $this->getSQL();
		$ptable = $this->getEn()->getEnMap()->getPhysicsTable();
		$pk = $this->getEn()->getPK();
		switch ($this->getEn()->getEnMap()->getEnDBUrl()->getDBType()) {
			case $Oracle:
				if ((($this->_sql == NULL) || $this->_sql->equals("")))
				{
					$sql = ((((("SELECT COUNT(" . $ptable) . ".") . $pk) . ") as C FROM ") . $ptable);
				}
				else
				{
					$sql = ((((("SELECT COUNT(" . $ptable) . ".") . $pk) . ") as C ") . $sql->substring($sql->indexOf("FROM ")));
				}
				break;
			default:
				if ((($this->_sql == NULL) || $this->_sql->equals("")))
				{
					$sql = ((((("SELECT COUNT(" . $ptable) . ".") . $pk) . ") as C FROM ") . $ptable);
				}
				else
				{
					$sql = $sql->substring($sql->indexOf("FROM "));
					if (($sql->indexOf("ORDER BY") >= 0))
						$sql = $sql->substring(0, ($sql->indexOf("ORDER BY") - 1));
					$sql = ((((("SELECT COUNT(" . $ptable) . ".") . $pk) . ") as C ") . $sql);
				}
				break;
		}
		try 
		{
			$i = DBAccess->RunSQLReturnValInt($sql, $this->getMyParas());
			if (($this->getTop() == -1))
			{
				return $i;
			}
			if (($this->getTop() >= $i))
			{
				return $i;
			}
			else
			{
				return $this->getTop();
			}
		}
		catch (\RuntimeException $ex)
		{
			if ($SystemConfig->getIsDebug())
			{
				try 
				{
					$this->getEn()->CheckPhysicsTable();
				}
				catch (Exception $e)
				{
					$e->printStackTrace();
				}
			}
			throw $ex;
		}
		catch (Exception $e)
		{
			$e->printStackTrace();
			return -1;
		}
	}
	public function DoGroupQueryToTable ($selectSQl, $groupBy, $orderBy) // [String selectSQl, String groupBy, String orderBy]
	{
		$sql = $this->getSQL();
		$ptable = $this->getEn()->getEnMap()->getPhysicsTable();
		switch ($this->getEn()->getEnMap()->getEnDBUrl()->getDBType()) {
			case $Oracle:
				if ((($this->_sql == NULL) || $this->_sql->equals("")))
					$sql = ((((($selectSQl . " FROM ") . $ptable) . "WHERE ") . $groupBy) . $orderBy);
				else
					$sql = ((($selectSQl . $sql->substring($sql->indexOf(" FROM "))) . $groupBy) . $orderBy);
				break;
			default:
				if ((($this->_sql == NULL) || $this->_sql->equals("")))
					$sql = ((((($selectSQl . " FROM ") . $ptable) . "WHERE ") . $groupBy) . $orderBy);
				else
				{
					$sql = $sql->substring($sql->indexOf(" FROM "));
					if (($sql->indexOf("ORDER BY") >= 0))
						$sql = $sql->substring(0, ($sql->indexOf("ORDER BY") - 1));
					$sql = ((($selectSQl . $sql) . $groupBy) . $orderBy);
				}
				break;
		}
		return DBAccess->RunSQLReturnTable($sql, $this->getMyParas());
	}
	public function DoQueryToTable_I ($topNum) // [int topNum]
	{
		return DBAccess->RunSQLReturnTable($this->getSQL(), $this->getMyParas());
	}
	protected function doEntityQuery () 
	{
		return DBAccess->RunSQLReturnResultSet($this->getSQL(), $this->getMyParas(), $this->getEn(), $this->getEn()->getEnMap()->getAttrs());
	}
	protected function doEntitiesQuery () 
	{
		if ((($SystemConfig->getAppCenterDBType() == $DBType->Oracle) && ($this->getTop() != -1)))
		{
			$this->addAnd();
			$this->AddWhereField_String_String_I("RowNum", "<=", $this->getTop());
		}
		return DBAccess->RunSQLReturnResultSet($this->getSQL(), $this->getMyParas(), $this->getEns(), $this->getEn()->getEnMap()->getAttrs());
	}
	public static function InitEntitiesByDataTable ($ens, $dt, $fullAttrs) // [Entities ens, DataTable dt, String[] fullAttrs]
	{
		$isUpper =  FALSE ;
		if (($SystemConfig->getAppCenterDBType() == $DBType->Oracle))
			$isUpper =  TRUE ;
		if (($fullAttrs == NULL))
		{
			$enMap = $ens->getNewEntity()->getEnMap();
			$attrs = $enMap->getAttrs();
			try 
			{
				foreach ($dt->Rows as $dr) 				{
					$en = $ens->getNewEntity();
					foreach ($attrs as $attr) 					{
						if (($isUpper ==  TRUE ))
							$en->SetValByKey_String_F($attr->getKey(), $dr->getValue($attr->getKey()->toUpperCase()));
						else
							$en->SetValByKey_String_F($attr->getKey(), $dr->getValue($attr->getKey()));
					}
			/* match: Entity */
					$ens->AddEntity_Entity($en);
				}
			}
			catch (\RuntimeException $ex)
			{
				$cols = "";
				foreach ($dt->Columns as $dc) 				{
					$cols += (" , " . $dc->ColumnName);
				}
				throw new \RuntimeException(((((("Columns=" . $cols) . "@Ens=") . $ens) . " @异常信息:") . $ex->getMessage()));
			}
			return $ens;
		}
		foreach ($dt->Rows as $dr) 		{
			$en = $ens->getNewEntity();
			foreach ($fullAttrs as $str) 			{
				if (($isUpper ==  TRUE ))
					$en->SetValByKey_String_F($str, $dr->getValue($str->toUpperCase()));
				else
					$en->SetValByKey_String_F($str, $dr->getValue($str));
			}
			/* match: Entity */
			$ens->AddEntity_Entity($en);
		}
		return $ens;
	}
}
?>
